package us.gaaoc.framework.webapp.backingbeans.admin;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.faces.component.UIParameter;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;

import org.apache.log4j.Logger;
import org.hibernate.Hibernate;
import org.springframework.transaction.annotation.Transactional;

import us.gaaoc.framework.model.Person;
import us.gaaoc.framework.webapp.backingbeans.BaseBackingBean;
import us.gaaoc.framework.webapp.backingbeans.ClientPage;

public class UserBrowser extends BaseBackingBean {

	private transient Logger log = Logger.getLogger(UserBrowser.class);
	

	/**
	 * Properties 
	 */
	private DataModel allPeople;
	private Person personDetails;
	private UIParameter clickedPerson;	
	
	/**
	 * Constructor
	 *
	 */
	public UserBrowser() {
		List<Person> allPeopleList = new ArrayList<Person>();
		String courtUuid = (String) this.getFacesContext().getExternalContext().getSessionMap().get(
				"courtUuid");
		allPeopleList = this.getModelFacade().getNonDeletedPersonWithNonNullUserId(courtUuid);
		this.allPeople = new ListDataModel();
		this.allPeople.setWrappedData(allPeopleList);
	}
	
	/**
	 * @return Returns the clickUuid.
	 */
	public UIParameter getClickedPerson() {
		return clickedPerson;
	}

	public String masquarade() {
		this.personDetails = (Person) this.clickedPerson.getValue();
		
		ClientPage clientPage = new ClientPage();
		clientPage.setUsername(personDetails.getUserId());
		clientPage.setPassword(personDetails.getPassword());

						
		return null;
	}
	
	@Transactional
	public void toggleCvNumber()
	{
		this.personDetails = (Person) this.clickedPerson.getValue();
		
		this.personDetails.setSetCivilActionNumber(
			! this.personDetails.isSetCivilActionNumber() );			
	}
	
	@Transactional
	public void toggleCourtDate()
	{
		this.personDetails = (Person) this.clickedPerson.getValue();
		
		this.personDetails.setSetCourtDate(
			! this.personDetails.isSetCourtDate() );			
	}
	
	/**
	 * @param clickUuid The clickUuid to set.
	 */
	public void setClickedPerson(UIParameter clickUuid) {
		this.clickedPerson = clickUuid;
	}

	/**
	 * This method navigates user to the view details page
	 * @return String
	 */
	public String viewDetails(){
		Map sessionMap = this.getFacesContext().getExternalContext().getSessionMap();
		this.personDetails = (Person) this.clickedPerson.getValue();
		UserDetailsPage userDetails = new UserDetailsPage();
		userDetails.setPersonDetails(this.personDetails);
		sessionMap.put("userDetails",userDetails);
		
		return "viewDetails";
	}

	/**
	 * @return Returns the personDetails.
	 */
	public Person getPersonDetails() {
		Hibernate.initialize(this.personDetails);
		return personDetails;
	}
	
	/**
	 * Mathod to navigate back to the admin page
	 * @return
	 */
	public String back(){
		return "adminConsole";
	}

	
	/**
	 * @return Returns the allPeople.
	 */
	public DataModel getAllPeople() {
		return allPeople;
	}
}